package sh.wnxy.wn38.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sh.wnxy.wn38.entity.Comment;
import sh.wnxy.wn38.entity.Img;
import sh.wnxy.wn38.entity.Spot;
import sh.wnxy.wn38.service.ICommentService;
import sh.wnxy.wn38.service.IImgService;
import sh.wnxy.wn38.service.ISpotService;
import sh.wnxy.wn38.utils.Result;
import sh.wnxy.wn38.service.vo.SpotQuery;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wn38
 * @since 2021-07-14
 */
@Slf4j
@ApiModel("景点管理")
@CrossOrigin
@RestController
@RequestMapping("/spot")
public class SpotController {

    private ISpotService iSpotService;

    private IImgService iImgService;

    @Autowired
    private ICommentService iCommentService;

    @Autowired
    public void setiSpotService(ISpotService iSpotService) {
        this.iSpotService = iSpotService;
    }

    @Autowired
    public void setiImgService(IImgService iImgService) {
        this.iImgService = iImgService;
    }


    @ApiOperation("根据景点编号删除景点")
    @GetMapping("/removeSpot/{id}")
    public Result removeSpot(@ApiParam(value = "景点编号", required = true) @PathVariable("id") String spotId) {
        boolean b = iSpotService.removeById(spotId);
        return b ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "新增景点")
    @PostMapping("addSpot")
    public Result addSpot(@ApiParam(value = "新增的景点具体信息") @RequestBody Spot spot) {
        boolean save = iSpotService.save(spot);
        return Result.ok().success(save).msg(save ? "成功" : "失败");
    }

    @ApiOperation(value = "修改景点")
    @PostMapping("updateSpot")
    public Result updateSpot(@ApiParam(value = "修改的景点具体信息") @RequestBody Spot spot) {
        boolean update = iSpotService.updateById(spot);
        return update ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "分页显示景点信息")
    @GetMapping("/listSpotByPage/{pageNum}/{pageSize}")
    public Result listSpotByPage(@ApiParam(value = "当前页码数", required = true) @PathVariable("pageNum") int pageNum,
                                 @ApiParam(value = "每页条数", required = true) @PathVariable("pageSize") int pageSize) {

        Page<Spot> pageSpot = new Page<>(pageNum, pageSize);
        iSpotService.page(pageSpot);

        long total = pageSpot.getTotal(); //-- 总记录数
        List<Spot> records = pageSpot.getRecords();  //-- 数据集合

        return Result.ok().data("total", total).data("list", records);
    }

    @ApiOperation("根据景点编号获取景点信息")
    @PostMapping("/findSpotById/{id}")
    public Result findSpotById(@ApiParam(value = "景点编号", required = true) @PathVariable("id") String spotId) {
        Spot spot = iSpotService.getById(spotId);
        return Result.ok().data("spot", spot);
    }

    @ApiOperation("根据景点编号获取景点图片")
    @PostMapping("/findSpotImgById/{id}")
    public Result findSpotImgById(@ApiParam(value = "景点编号", required = true) @PathVariable("id") Integer spotId) {
        log.warn(spotId + "");
        List<Img> imgs = iImgService.listImgBySpotId(spotId);
        return Result.ok().data("imgList", imgs);
    }

    @ApiOperation(value = "根据条件分页查询景点")
    @PostMapping("/pageListSpot/{currentPage}/{pageSize}")
    public Result pageListSpot(@ApiParam(value = "当前页码", name = "currentPage", required = true) @PathVariable("currentPage") Integer currentPage,
                               @ApiParam(value = "每页显示记录数", name = "pageSize", required = true) @PathVariable("pageSize") Integer pageSize,
                               @ApiParam(value = "查询条件", name = "spotQuery", required = false) @RequestBody SpotQuery spotQuery) {
        Page<Spot> pageSpot = new Page<>(currentPage, pageSize);

        //-- 构建查询条件类。MyBatis Plus的类，
        QueryWrapper<Spot> wrapper = new QueryWrapper<>();

        //-- 获取具体的查询信息
        String name = spotQuery.getName();
        System.out.println("查询条件：" + name);
        //-- 根据实际的参数情况完成QueryWrapper的赋值
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("spot_name", name);
        }


        wrapper.orderByAsc("spot_queue");

        //-- 进行分页查询的方法
        iSpotService.page(pageSpot, wrapper);

        long total = pageSpot.getTotal(); //-- 总记录数
        List<Spot> records = pageSpot.getRecords();  //-- 数据集合

        return Result.ok().data("total", total).data("list", records);
    }

    @ApiOperation(value = "查询景点评分")
    @GetMapping("/getSpotScore/{spotId}")
    public Result getSpotScore(@PathVariable("spotId") Integer spotId) {
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("comt_spotid", spotId);
        List<Comment> list = iCommentService.list(commentQueryWrapper);
        Double averageScore = 0.0;
        Double sum = 0.0;
        Integer count = 0;
        for (Comment c : list) {
            Double comtScore = c.getComtScore();
            sum += comtScore;
            count++;
        }
        return Result.ok().data("averageScore", sum / count);
    }

}

